package com.atguigu.tingshu.album.receiver;


import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


@Slf4j
@Component
public class AlbumReceiver {

    @Autowired
    private TrackInfoService trackInfoService;
    @Autowired
    private RedisTemplate redisTemplate;


    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value =MqConst.EXCHANGE_TRACK ,durable = "true"),
            value = @Queue (value =MqConst.QUEUE_TRACK_STAT_UPDATE ,durable = "true"),
            key= MqConst.ROUTING_TRACK_STAT_UPDATE
    ))
    public void updateTrackStat(TrackStatMqVo mqVo, Channel channel, Message message){
     if(mqVo !=null){
         log.info("更新声音统计: {}",mqVo);
     }

        String redisKey = RedisConstant.USER_TRACK_REPEAT_STAT_PREFIX + "db:" + mqVo.getBusinessNo();
        Boolean flag = redisTemplate.opsForValue().setIfAbsent(redisKey, "1", 10, TimeUnit.SECONDS);
        if(flag){
            try {
                trackInfoService.updateTrackStat(mqVo);
            } catch (Exception e) {
                redisTemplate.delete(redisKey);
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            }

        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}

